using System;
using Headspring.Training.DataAccess.Impl;
using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Services;
using NHibernate;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace Headspring.Training.DataAccess.IntegrationTests
{
	[TestFixture]
	public class EmployeeRepositoryTester : RepositoryBase
	{
		[Test]
		public void ShouldGetAllEmployees()
		{
			TestHelper.ClearTables();

			ISession session = getSession();
			Employee one = new Employee("1", "first1", "last1", "email1");
			one.SetNotificationPreferenceFor(DayOfWeek.Friday, NotificationType.SMS);
			Employee two = new Employee("2", "first2", "last2", "email2");
			Employee three = new Employee("3", "first3", "last3", "email3");
			session.SaveOrUpdate(two);
			session.SaveOrUpdate(three);
			session.SaveOrUpdate(one);
			session.Flush();
			session.Dispose();

			IEmployeeRepository repository = new EmployeeRepository();
			Employee[] employees = repository.GetAllEmployees();

			Assert.That(employees.Length, Is.EqualTo(3));
			Assert.That(employees[0].UserName, Is.EqualTo("1"));
			Assert.That(employees[0].FirstName, Is.EqualTo("first1"));
			Assert.That(employees[0].LastName, Is.EqualTo("last1"));
			Assert.That(employees[0].EmailAddress, Is.EqualTo("email1"));
			Assert.That(employees[0].GetNotificationTypeFor(DayOfWeek.Friday), Is.EqualTo(NotificationType.SMS));
			Assert.That(employees[0].GetNotificationTypeFor(DayOfWeek.Monday), Is.EqualTo(NotificationType.Both));
			Assert.That(employees[1].GetNotificationTypeFor(DayOfWeek.Friday), Is.EqualTo(NotificationType.Both));
			Assert.That(employees[1].GetNotificationTypeFor(DayOfWeek.Monday), Is.EqualTo(NotificationType.Both));
		}

		[Test]
		public void ShouldFindEmployeeByUsername()
		{
			TestHelper.ClearTables();

			ISession session = getSession();
			Employee one = new Employee("1", "first1", "last1", "email1");
			one.SetNotificationPreferenceFor(DayOfWeek.Monday, NotificationType.None);
			one.SetNotificationPreferenceFor(DayOfWeek.Tuesday, NotificationType.Email);
			one.SetNotificationPreferenceFor(DayOfWeek.Wednesday, NotificationType.SMS);
			one.SetNotificationPreferenceFor(DayOfWeek.Thursday, NotificationType.Both);
			one.SetNotificationPreferenceFor(DayOfWeek.Friday, NotificationType.None);
			one.SetNotificationPreferenceFor(DayOfWeek.Saturday, NotificationType.Email);
			one.SetNotificationPreferenceFor(DayOfWeek.Sunday, NotificationType.SMS);
			Employee two = new Employee("2", "first2", "last2", "email2");
			Employee three = new Employee("3", "first3", "last3", "email3");
			session.SaveOrUpdate(one);
			session.SaveOrUpdate(two);
			session.SaveOrUpdate(three);
			session.Flush();
			session.Dispose();

			IEmployeeRepository repository = new EmployeeRepository();
			Employee employee = repository.GetByUserName("1");
			Assert.That(employee.Id, Is.EqualTo(one.Id));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Monday), Is.EqualTo(NotificationType.None));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Tuesday), Is.EqualTo(NotificationType.Email));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Wednesday), Is.EqualTo(NotificationType.SMS));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Thursday), Is.EqualTo(NotificationType.Both));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Friday), Is.EqualTo(NotificationType.None));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Saturday), Is.EqualTo(NotificationType.Email));
			Assert.That(employee.GetNotificationTypeFor(DayOfWeek.Sunday), Is.EqualTo(NotificationType.SMS));
		}
	}
}